package com.bigflexlib.x.flash.geom
{
	
	
	
	import com.bigflexlib.x.flash.geom.Point.PolarPoint;
	
	import flash.geom.Matrix;
	import flash.geom.Point;
	
	import mx.utils.StringUtil;
	
	
	
	public class WarpPathTest extends GeomTestBase
	{
		
		
		private static const 	pi4		:Number = Math.PI / 4;
		
		private static const p_1_pi4	:PolarPoint = new PolarPoint( 1, pi4 );
		
		
		
		
		public function testMisc( ):void
		{
		
			var start	:Matrix;
			var end		:Matrix;
			var anchor	:Point;
			var wp		:WarpPath;
		
			var t1		:Transformation2D;
			var t2		:Transformation2D;

			


			////////////////////////////////////////////////////////////////////
			// 0, 0  -->  10, 10  ( anchor 0,0 )
			////////////////////////////////////////////////////////////////////
			start 	= new Matrix( );
			end		 = new Matrix( );
			end.translate( 10, 10 );
			
			wp = new WarpPath( start, new Point( 0, 0 ), end );
			
			// 0
			///////////////////////////////
			assertStep( wp, 0, new Transformation2D( 0, 0, 1, 1, 0 ) );

			// 0.5
			///////////////////////////////
			assertStep( wp, 0.5, new Transformation2D( 5, 5, 1, 1, 0 ) );

			// 0.8
			///////////////////////////////
			assertStep( wp, 0.8, new Transformation2D( 8, 8, 1, 1, 0 ) );	

			// 1
			///////////////////////////////
			assertStep( wp, 1, new Transformation2D( 10, 10, 1, 1, 0 ) );	






			////////////////////////////////////////////////////////////////////
			// 0, 1  -->  0, 11  anchor ( 0, 1 )
			////////////////////////////////////////////////////////////////////
			start 	= new Matrix( );
			start.translate( 0, 1 );
			end		 = new Matrix( );
			end.translate( 0, 11 );
			
			wp = new WarpPath( start, new Point( 0, 1 ), end ); // the anchor is the origin of start system
				
			// internal state examination	
			assertEquals( wp.rotationDelta 		, 0 );
			assertEquals( wp.scaleFactor.factor , 1 );					
			assertPointsSimilar( wp.absoluteStartAnchor, new Point( 0, 1 ) );
			assertPointsSimilar( wp.absoluteEndAnchor, new Point( 0, 11 ) );
			
			
			// anchor position for step
			assertPointsSimilar( wp.getAnchorPositionForStep( 0 ), 		new Point( 0, 1 ) );
			assertPointsSimilar( wp.getAnchorPositionForStep( 0.5 ), 	new Point( 0, 6 ) );
			assertPointsSimilar( wp.getAnchorPositionForStep( 1 ), 		new Point( 0, 11 ) );
			
			
			
			// 0
			///////////////////////////////
			assertStep( wp, 0, new Transformation2D( 0, 1, 1, 1, 0 ) );

			// 0.5
			///////////////////////////////
			assertStep( wp, 0.5, new Transformation2D( 0, 6, 1, 1, 0 ) );

			// 0.8
			///////////////////////////////
			assertStep( wp, 0.8, new Transformation2D( 0, 9, 1, 1, 0 ) );	

			// 1
			///////////////////////////////
			assertStep( wp, 1, new Transformation2D( 0, 11, 1, 1, 0 ) );	


	
	
	
	
			////////////////////////////////////////////////////////////////////
			// no changes
			////////////////////////////////////////////////////////////////////
			start 	= new Matrix( );
			end		= new Matrix( );
			
			wp = new WarpPath( start, p_1_pi4 , end );
				
			// internal state examination	
			assertEquals( wp.rotationDelta 		, 0 );
			assertEquals( wp.scaleFactor.factor , 1 );					
			assertPointsSimilar( wp.absoluteStartAnchor, 	p_1_pi4 );
			assertPointsSimilar( wp.absoluteEndAnchor, 		p_1_pi4 );
			
			// anchor position for step
			assertPointsSimilar( wp.getAnchorPositionForStep( 0 ), 		p_1_pi4 );
			assertPointsSimilar( wp.getAnchorPositionForStep( 0.5 ), 	p_1_pi4 );
			assertPointsSimilar( wp.getAnchorPositionForStep( 1 ), 		p_1_pi4 );
			
			// scaling for step
			assertEquals( wp.getScalingForStep( 0 )		, 1 );
			assertEquals( wp.getScalingForStep( 0.1 )	, 1 );
			assertEquals( wp.getScalingForStep( 1 )		, 1 );

			// rotation for step
			assertEquals( wp.getRotationForStep( 0 )		, 0 );
			assertEquals( wp.getRotationForStep( 0.1 )		, 0 );
			assertEquals( wp.getRotationForStep( 1 )		, 0 );


			
			assertStep( wp, 0, 		Transformation2D.IDENTITY );
			assertStep( wp, 0.5, 	Transformation2D.IDENTITY );
			assertStep( wp, 1, 		Transformation2D.IDENTITY );






			////////////////////////////////////////////////////////////////////
			// 
			////////////////////////////////////////////////////////////////////
			start 	= new Matrix( );
			end		= new Matrix( );
			end.scale( 11, 11 );
			

			
			wp = new WarpPath( start, new PolarPoint( 1, pi4 ), end );
				
			// internal state examination	
			assertEquals( wp.rotationDelta 		, 0 );
			assertEquals( wp.scaleFactor.factor , 11 );					
			assertPointsSimilar( wp.absoluteStartAnchor, new PolarPoint( 1, pi4 ) );
			assertPointsSimilar( wp.absoluteEndAnchor, new PolarPoint( 11, pi4 ) );
			
			
			// anchor position for step
			assertPointsSimilar( wp.getAnchorPositionForStep( 0 ), 		new PolarPoint( 1, pi4 ) );
			assertPointsSimilar( wp.getAnchorPositionForStep( 0.1 ), 	new PolarPoint( 2, pi4 ) );
			assertPointsSimilar( wp.getAnchorPositionForStep( 0.5 ), 	new PolarPoint( 6, pi4 ) );
			assertPointsSimilar( wp.getAnchorPositionForStep( 1 ), 		new PolarPoint( 11, pi4 ) );
			
			// scaling for step
			assertEquals( wp.getScalingForStep( 0 )		, 1 );
			assertEquals( wp.getScalingForStep( 0.1 )	, 2 );
			assertEquals( wp.getScalingForStep( 1 )		, 11 );

			// scaling for step
			assertEquals( wp.getRotationForStep( 0 )		, 0 );
			assertEquals( wp.getRotationForStep( 0.1 )		, 0 );
			assertEquals( wp.getRotationForStep( 1 )		, 0 );





			////////////////////////////////////////////////////////////////////
			// upscaling tests
			////////////////////////////////////////////////////////////////////
			start 	= new Matrix( );
			start.scale( 2, 2 );
			end		= new Matrix( );
			end.scale( 22, 22 );

			
			wp = new WarpPath( start, new PolarPoint( 1, pi4 ), end );
				
			// internal state examination	
			assertEquals( wp.rotationDelta 		, 0 );
			assertEquals( wp.scaleFactor.factor , 11 );					
			assertPointsSimilar( wp.absoluteStartAnchor, 	new PolarPoint( 1, pi4 ) 	);
			assertPointsSimilar( wp.absoluteEndAnchor, 		new PolarPoint( 11, pi4 ) 	);
			
			
			// anchor position for step
			assertPointsSimilar( wp.getAnchorPositionForStep( 0 ), 		new PolarPoint( 1, pi4 ) );
			assertPointsSimilar( wp.getAnchorPositionForStep( 0.1 ), 	new PolarPoint( 2, pi4 ) );
			assertPointsSimilar( wp.getAnchorPositionForStep( 0.5 ), 	new PolarPoint( 6, pi4 ) );
			assertPointsSimilar( wp.getAnchorPositionForStep( 1 ), 		new PolarPoint( 11, pi4 ) );
			
			// scaling for step
			assertEquals( wp.getScalingForStep( 0 )		, 2 );
			assertEquals( wp.getScalingForStep( 0.1 )	, 4 );
			assertEquals( wp.getScalingForStep( 1 )		, 22 );

			// scaling for step
			assertEquals( wp.getRotationForStep( 0 )		, 0 );
			assertEquals( wp.getRotationForStep( 0.1 )		, 0 );
			assertEquals( wp.getRotationForStep( 1 )		, 0 );
			
			

		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		private function assertStep( warp:WarpPath, step:Number, transformation:Transformation2D ):void
		{
			
			var t:Transformation2D = warp.getTransformationForStep( step );
			
			if ( ! t.similar( transformation ) )
			{
				fail( StringUtil.substitute( "Transformation: expected ({0}) but found ({1})", transformation, t ) );
			}
				
		
		}
		
		
		
		
		
	}

}